package com.hectopascal.goalance.controller;

import com.hectopascal.goalance.pojo.Department;
import com.hectopascal.goalance.pojo.dto.ResponseMessage;
import com.hectopascal.goalance.service.Ipml.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    IDepartmentService departmentService;

    //获取所有部门
    @GetMapping("/getdepartments")
    public ResponseMessage<Map<String, Object>> departmentGet(@RequestParam Integer page,
                                                              @RequestParam Integer size,
                                                              @RequestParam(required = false) String name,
                                                              @RequestParam(required = false) Integer status) {
        return ResponseMessage.success(departmentService.departmentsGet(page, size, name, status));
    }

    //添加新部门
    @PostMapping("/adddepartment")
    public ResponseMessage<Department> departmentAdd(@RequestBody Department department) {
        Department department1 = departmentService.departmentAdd(department);
        return ResponseMessage.success(department1);
    }

    //修改部门信息
    @PutMapping("/updatedepartment")
    public ResponseMessage<String> departmentUpdate(@RequestBody Department department) {
        int rows=departmentService.departmentUpdate(department);
        if(rows>0){
            return ResponseMessage.success("修改成功");
        }else{
            return ResponseMessage.fail("修改失败");
        }
    }

    //删除部门
    @DeleteMapping("/deletedepartment/{id}")
    public ResponseMessage<String> departmentDelete(@PathVariable Integer id) {
        int rows=departmentService.departmentDelete(id);
        if(rows>0){
            return ResponseMessage.success("删除成功");
        }else{
            return ResponseMessage.fail("删除失败");
        }
    }
}
